<template>
	<view class="store-manage">
		<gyd-navbar back-color="#fff" :title="title"></gyd-navbar>

		<view class="store-list">
			<view class="store-item" v-for="(item,index) in list" :key="index">
				<!-- <view class="title">
					<view class="left">店铺类型：<text>{{item.store_type_str}}</text></view>
					<view class="right">店铺状态：<text>{{item.status_str}}</text></view>
				</view> -->
				<view class="content">
					<image :src="item.store_logo"></image>
					<view class="info">
						<view>店铺名称：{{item.store_name || '待完善'}}</view>
						<view>联系人：{{item.contact_name || '待完善'}}</view>
						<view>联系电话：{{item.contact_phone || '待完善'}}</view>
						<view>店铺地址：{{item.street_address || '待完善'}}</view>
						<!-- <view>入驻时间：{{item.create_time || '待完善'}}</view> -->
					</view>
				</view>
				<view class="opt-btn">
					<button type="primary" size="mini" @click="edit(item.id)">编辑</button>
					<!-- <button type="warn" size="mini" @click="del(item.id)">删除</button> -->
					<button type="warn" size="mini" @click="selStore(item)">选择</button>
				</view>
			</view>

			<view class="empty-data" v-if="list.length===0">暂未有店铺入驻</view>
		</view>

		<view class="footer">
			<view class="btn-group">
				<u-button type="primary" size="large" block
					@click="pageRoute('/merchantPages/store-manage/store-join?type=add')">连锁门店入驻</u-button>
			</view>
		</view>
	</view>
</template>

<script setup>
	import request from '@/utils/request';
	import cache from '@/utils/cache';
	import util from '@/utils/util';
	import {
		ref,
		reactive,
		toRefs,
		inject,
	} from 'vue';
	import {
		onLoad,
		onShow,
		onReady,
		onPullDownRefresh,
		onReachBottom,
	} from '@dcloudio/uni-app';

	const state = reactive({
		title: '门店管理',
		themeColor: cache.get('themeColor'),
		imgHost: util.requestUrl(),
		isMore: true,
		list: []
	});

	//在页面所有元素加载完成后执行（只执行一次）
	onLoad((opt) => {
		console.log('onLoad');
		if (opt.hasOwnProperty('id')) {
			console.log('传递参数', opt.id);
		}
		getStroeList();
	});

	//在页面由不可见变为可见时执行（只要页面显示都会执行）
	onShow(() => {
		console.log('onShow');
	})

	//在页面解析完成之后执行
	onReady(() => {
		console.log('onReady');
	})

	onPullDownRefresh(() => {
		console.log('下拉刷新');
		setTimeout(() => {
			uni.stopPullDownRefresh();
		}, 1000);
	})

	onReachBottom(() => {
		console.log('触底加载更多');
	})

	function getStroeList() {
		/* state.list = [{
			id: 1,
			store_name: '隔云端水站',
			store_logo: '',
			contact_name: '王云海',
			store_phone: '18187058913',
			store_adress: '',
			create_time: '2025-10-07 22:44:15',
			store_type_str: '水站',
			status_str: '待审核',
		}]; */
		request.get('merchant.merchant/storeList', {
			skip: state.list.length,
		}).then(res => {
			console.log('店铺列表', res);
			state.isMore = res.data.is_more;
			if (state.list.length == 0) {
				state.list = res.data.data;
			} else {
				state.list = state.list.concat(res.data.data);
			}
		})
	}

	function edit(id) {
		pageRoute(`/merchantPages/store-manage/store-join?type=modify&id=${id}`);
	}

	function del(id) {
		console.log('删除店铺', id);
	}

	function selStore(item) {
		request.post('store/switchStore', {
			store_id: item.id
		}).then(res => {
			console.log('选择店铺', res);
			if (res.code == 1) {
				setTimeout(() => {
					cache.set('selStore', item);
					pageTabRoute('/merchantPages/profile/profile');
				}, 800);
			} else {
				util.showTip(res.msg);
			}
		})
	}

	function pageRoute(url) {
		uni.navigateTo({
			url
		});
	}

	function pageTabRoute(url) {
		uni.reLaunch({
			url
		});
	}

	const {
		title,
		themeColor,
		imgHost,
		isMore,
		list,
	} = toRefs(state);
</script>

<style lang="scss">
	.store-manage {
		.store-list {
			padding-bottom: 50px;
			position: relative;
			padding: $m24;

			.store-item {
				margin-bottom: $m24;
				padding: $m24;
				background-color: #fff;
				border-radius: $radius-16;

				.title {
					display: flex;
					justify-content: space-between;
					padding: 10rpx 20rpx;

					.left {
						// padding-left: 20rpx;
					}

					.right {
						// padding-right: 20rpx;
					}
				}

				.content {
					display: flex;
					padding: 10rpx;

					image {
						width: 200rpx;
						height: 200rpx;
					}

					.info {
						padding-left: 20rpx;
					}
				}

				.opt-btn {
					display: flex;
				}
			}

			.empty-data {
				margin-bottom: $m24;
				padding: $m24;
				background-color: #fff;
				border-radius: $radius-16;
				text-align: center;
				align-items: center;
				justify-content: center;
			}
		}

		.footer {
			position: fixed;
			/* 固定在底部 */
			bottom: 0;
			width: 100%;

			.btn-group {}
		}
	}
</style>